<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <header th:replace="header::html"></header>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">部门名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deptName" placeholder="请输入部门名称" class="layui-input">
                </div>
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="status">
                        <option value=""></option>
                        <option value="1">正常</option>
                        <option value="0">禁用</option>
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dept-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dept-table" lay-filter="dept-table"></table>
    </div>
</div>

<script type="text/html" id="dept-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开全部
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠全部
    </button>
</script>

<script type="text/html" id="dept-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>


<script type="text/html" id="dept-status">
    <input type="checkbox"  name="status"  value="{{d.deptId}}" lay-skin="switch" lay-text="启用|禁用" mid="{{d.deptId}}"  lay-filter="dept-status" {{ d.status == '1' ? 'checked' : '' }}>
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>
<script type="text/html" id="dept-time">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script>
    layui.use(['table','form','jquery','treetable'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;


        var insTb  =  treetable.render({
            treeColIndex: 1,//树形图标显示在第几列
            treeSpid: 0,//最上级的父级id
            treeIdName: 'deptId', //id字段的名称
            treePidName: 'parentId',//父级节点字段
            skin:'line',
            method:'post',
            // treeDefaultClose: true,
            toolbar:'#dept-toolbar',
            elem: '#dept-table',
            url: '/api/dept',
            page: false,
            cols: [
                [
                    {type: 'radio'},
                    {field: 'deptName', minWidth: 200, title: '名称'},
                    {field: 'sort', title: '排序'},
                    {field: 'status', title: '是否可用',templet:'#dept-status'},
                    {title: '创建时间', field: 'createTime', align:'center',templet:'#dept-time'},
                    {field: 'updateTime', title: '更新时间', align:'center',templet:'#dept-time'},
                    {title: '操作',templet: '#dept-bar', width: 150, align: 'center'}
                ]
            ]
        });

        table.on('tool(dept-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })

        table.on('toolbar(dept-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'expandAll'){
                treetable.expandAll("#dept-table");
            } else if(obj.event === 'foldAll'){
                treetable.foldAll("#dept-table");
            }
        });
        form.on('switch(dept-status)', function(obj) {
            layer.confirm(obj.elem.checked ? '确定要启用该部门吗' : '确定要停用该部门吗', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                var status = obj.elem.checked ? 1 : 0;
                var data ={
                    deptId: obj.value,
                    status: status
                }
                let loading = layer.load();
                $.ajax({
                    url:'/api/dept/changeStatus',
                    data:JSON.stringify(data),
                    dataType:'json',
                    contentType:'application/json',
                    type:'put',
                    success:function(result){
                        layer.close(loading);
                        if(result.success){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                obj.del();
                                layui.table.reload("dept-table");
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                            layui.table.reload("dept-table");
                        }
                    }
                })
            });
        });
        form.on('submit(dept-query)', function(data){ //模糊查询方法
            var formData = data.field;
            var name = formData.name;
            var status = formData.status;
            table.reload(('dept-table'),{ // table重载
                treeColIndex: 1,//树形图标显示在第几列
                treeSpid: 0,//最上级的父级id
                treeIdName: 'deptId', //id字段的名称
                treePidName: 'parentId',//父级节点字段
                skin:'line',
                treeDefaultClose: true,
                toolbar:'#dept-toolbar',
                elem: '#dept-table',
                where: {//这里传参  向后台
                    name: name,
                    status: status
                    //可传多个参数到后台...  ，分隔
                }
                , url: '/api/dept'//后台做模糊搜索接口路径
                , method: 'get'
            });
            return false;
        });
        window.add = function(){
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/api/dept/add'
            });
        }

        window.edit = function(obj){
            var data = obj.data;
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/api/dept/edit/?deptId='+data.deptId
            });
        }
        window.remove = function(obj){
            var data = obj.data;
            layer.confirm('确定删除吗,此操作不能撤销！', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/api/dept/?deptId=" + data.deptId,
                    dataType:'json',
                    type:'delete',
                    success:function(result){
                        layer.close(loading);
                        if(result.success){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                obj.del();
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>